package com.isoftstone.disk.user.service.impl;


import com.isoftstone.disk.login.entity.PtUserEntity;
import com.isoftstone.disk.mybatis.flexigrid.PadingRstType;
import com.isoftstone.disk.mybatis.flexigrid.PagingBean;
import com.isoftstone.disk.mybatis.ztree.ZtreeEntity;
import com.isoftstone.disk.user.entity.SearUserEntity;
import com.isoftstone.disk.user.repository.UserDao;
import com.isoftstone.disk.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Locale;

@Service("userService")
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;

    @Override
    public PadingRstType<PtUserEntity> getUserInfolistByPage(SearUserEntity searUserEntity, PagingBean pagingBean) {
        pagingBean.dealSortName(PtUserEntity.class);
        PadingRstType<PtUserEntity> ptUserEntityPadingRstType = new PadingRstType<PtUserEntity>();
        ptUserEntityPadingRstType.setPage(pagingBean.getPage());
        List<PtUserEntity> list = userDao.queryUserInfoListPadding(searUserEntity,pagingBean);
        ptUserEntityPadingRstType.setRawRecords(list);
        Integer total = userDao.queryUserInfoTotal(searUserEntity);
        ptUserEntityPadingRstType.setTotal(total);
        ptUserEntityPadingRstType.putItems();

        return ptUserEntityPadingRstType;
    }
    @Resource
    private MessageSource messageSource;
    @Override
    public List<ZtreeEntity> queryroleZtreeList(String userUuid) {
        //查询角色列表
        List<ZtreeEntity> list = userDao.queryRoleZtreeList(userUuid);
        //是否被选重
        List<String> roleIdList = userDao.queryRoleListByUserId(userUuid);
        for(ZtreeEntity ztreeEntity:list){
            for(String  roleId: roleIdList){
                if(roleId.equals(ztreeEntity.getId())){
                    ztreeEntity.setChecked(true);
                    break;
                }
            }
        }
        //增加角色的根目录
        ZtreeEntity ztreeEntity = new ZtreeEntity();
        ztreeEntity.setId("0");
        Locale locale = LocaleContextHolder.getLocale();
        String message =  messageSource.getMessage("user.role.name",null,locale);
        ztreeEntity.setName(message);
        ztreeEntity.setpId("-1");
        list.add(ztreeEntity);
        return list;
    }

    @Override
    public void saveRefRoleIdByUserId(String roleIds, String userId) {
        //进行分割角色ID
        String[] roleArray = roleIds.split(",");
        //根据用户ID删除角角信息
        userDao.deleteRoleByUserId(userId);
        for(String roleId:roleArray){
            //插件用户和角色的关系
            userDao.insertRoleAndUserRef(userId,roleId);

        }
    }

    @Override
    public PtUserEntity findByUserName(String userName) {
        return userDao.findOne(userName);

    }

    @Override
    public void addUser(PtUserEntity user) {
        userDao.add(user);
    }
}
